package com.ymatou.iapi.trading.userquery.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.ymatou.iapi.trading.userquery.parameter.GetBuyerSearchOrderInfoBean;
import com.ymatou.iapi.trading.userquery.parameter.resp.GetBuyerSearchOrderInfoResp;
import com.ymatou.iapi.trading.userquery.parameter.resp.SearchOrderInfo;
import com.ymatou.iapi.trading.userquery.service.GetBuyerSearchOrderInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.mongo.query.Order;
import com.ymttest.database.mongo.config.MongoOrderConfig;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*搜索商品信息接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetBuyerSearchOrderInfo {
	private static GetBuyerSearchOrderInfoBean getbuyersearchorderinfoBean;
	private static GetBuyerSearchOrderInfoCall getbuyersearchorderinfoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("搜索商品信息接口");
	}

	@Before
	public void caseUp() {
		getbuyersearchorderinfoBean = new GetBuyerSearchOrderInfoBean();
		getbuyersearchorderinfoCall = new GetBuyerSearchOrderInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getbuyersearchorderinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {

		GetBuyerSearchOrderInfoResp actualResponse = getbuyersearchorderinfoCall
				.getBuyerSearchOrderInfoResp();
		List<SearchOrderInfo> actualOrders = actualResponse
				.getSearchOrderInfos();

		MongoTemplate template = null;
		try {
			template = new MongoOrderConfig().mongoTemplate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (int i = 0; i < actualOrders.size(); i++) {

			SearchOrderInfo expect = actualOrders.get(i);

			String prdId = expect.getProductId();

			Criteria c = Criteria.where("prds").elemMatch(
					Criteria.where("prdId").is(prdId));
			Order order = template.findOne(new Query(c), Order.class, "Order");

			DataUtil.verify(expect.getProductPicUrl(),
					order.getProducts().get(0).getPicUrl(),
					"验证 SearchOrderInfo.productPicUrl");
			DataUtil.verify(expect.getTitle(), order.getProducts().get(0)
					.getTitle(), "验证 SearchOrderInfo.title");

		}

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBuyerSearchOrderInfo_001() {
		Logger.start(true, "搜索商品信息成功,开始时间，结束时间为null");
		try {

			//int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			getbuyersearchorderinfoBean.setUserId(4085);
			getbuyersearchorderinfoBean.setProductNum(50);
			getbuyersearchorderinfoBean.setTitle("修改");
			getbuyersearchorderinfoCall.setData(getbuyersearchorderinfoBean);
			getbuyersearchorderinfoCall.callService();

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBuyerSearchOrderInfo_002() {
		Logger.start(true, "搜索商品信息成功");
		try {
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");

			getbuyersearchorderinfoBean.setUserId(userId);
			getbuyersearchorderinfoBean.setProductNum(3);
			getbuyersearchorderinfoBean.setTitle("auto");
			getbuyersearchorderinfoBean.setBeginTime(beginTime);
			getbuyersearchorderinfoBean.setEndTime(endTime);

			getbuyersearchorderinfoCall.setData(getbuyersearchorderinfoBean);
			getbuyersearchorderinfoCall.callService();

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBuyerSearchOrderInfo_003() {
		Logger.start(true, "userid 为 null ");
		try {
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");

			//getbuyersearchorderinfoBean.setUserId(userId);
			getbuyersearchorderinfoBean.setProductNum(3);
			getbuyersearchorderinfoBean.setTitle("auto");
			getbuyersearchorderinfoBean.setBeginTime(beginTime);
			getbuyersearchorderinfoBean.setEndTime(endTime);

			getbuyersearchorderinfoCall.setData(getbuyersearchorderinfoBean);
			getbuyersearchorderinfoCall.callService();

			//verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetBuyerSearchOrderInfo_004() {
		Logger.start(true, "ProductNum 为 null ");
		try {
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));

			String beginTime = YMTDateUtil.getBeforeOrNextDay(-10,
					"yyyy-MM-dd'T'HH:mm:ss");
			String endTime = YMTDateUtil.getBeforeOrNextDay(1,
					"yyyy-MM-dd'T'HH:mm:ss");

			getbuyersearchorderinfoBean.setUserId(userId);
			//getbuyersearchorderinfoBean.setProductNum(3);
			getbuyersearchorderinfoBean.setTitle("auto");
			getbuyersearchorderinfoBean.setBeginTime(beginTime);
			getbuyersearchorderinfoBean.setEndTime(endTime);

			getbuyersearchorderinfoCall.setData(getbuyersearchorderinfoBean);
			getbuyersearchorderinfoCall.callService();

			//verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}